#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Datetime: 2021/1/19 下午4:54
# @Author  : HUANG Xiong
# @Site    : 
# @File    : portfolio_asset_allocation.py
# @Software: PyCharm 

"""
脚本说明：组合的资产配置比例信息
"""
import pandas as pd
from quant_researcher.quant.performance_attribution.core_functions.holding_analysis.allocation_analysis import \
    holding_based_allocation


def get_portfolio_asset_allocation(fund_list, weight_list, end_date):
    """
    得到组合的资产配置信息

    :param list fund_list: 基金代码列表
    :param list weight_list: 基金权重列表
    :param str end_date: 计算日期，带杠字符串，如"2020-12-12"
    :return:
    """
    p_weight_df = pd.DataFrame({'fund_code': fund_list, 'fund_weight': weight_list})
    p_alloc_df = holding_based_allocation.get_asset_allocation(fund_list, end_date=end_date)
    if p_alloc_df is None:
        return
    p_alloc_df = p_alloc_df[['fund_code', 'stock', 'bond', 'cash', 'other']].set_index('fund_code')
    p_weight_df = p_weight_df.set_index('fund_code')

    p_df = p_alloc_df.multiply(p_weight_df['fund_weight'], axis=0)
    p_df = p_df.sum()
    p_df = p_df.reset_index()
    p_df = p_df.rename(columns={'index': 'asset_type', 0: 'portfolio_asset_weight'})

    return p_df
